package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Component
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    @ResponseBody
    public R handerException(SQLIntegrityConstraintViolationException e){
        e.printStackTrace();//异常信息
        String message = e.getMessage();
        String[] split = message.split("'");
        return R.error("账号"+split[1]+"已存在");
    }

    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public R exceptionHandler(CustomException e){
        log.info(e.getMessage());
        return R.error(e.getMessage());
    }


}
